package org.gitman.agentplatform.ui.actions;

import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.JTable;

import org.gitman.agentplatform.agent.api.InternalAgentInfo;
import org.gitman.agentplatform.core.AgentManager;
import org.gitman.agentplatform.ui.simulation.AgentTableModel;

public class RemoveAgentAction extends UIAction{

	public RemoveAgentAction() {
		super("Remove");
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		JTable table = mainFrame.getSimulationPanel().getTable();
		int [] tab = table.getSelectedRows();
		AgentTableModel model = (AgentTableModel)table.getModel();
		ArrayList<Integer> list = new ArrayList<Integer>(tab.length);
		AgentManager agentManager = platformManager.getSimulation().getAgentManager();
		for(int i = 0;i<tab.length;i++){
			list.add((Integer)model.getValueAt(tab[i], 0));
		}
		for(Integer id : list){		
			InternalAgentInfo info = agentManager.getAgentInfo(id);
			Integer vID = info.getCurrentVertexID() != null ? info.getCurrentVertexID() : info.getStartVertexID();
			agentManager.removeAgent(id);
			platformManager.getView().colourVertex(vID, null);
		}			
		model.fireTableDataChanged();
	}

}
